<template>
	<el-container>
		<el-header style="height: auto;">
			<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-position" @click="sendSms">发送</el-button>
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="smsId" @selection-change="selectionChange" stripe>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="#" prop="sortNo" width="80">
					<template #default="scope">
						{{ scope.$index+1 }}
					</template>
				</el-table-column>
				<el-table-column label="消息类型" prop="module" width="100"></el-table-column>
				<el-table-column label="接收方" prop="toUserName" width="100"></el-table-column>
				<el-table-column label="手机号码" prop="iphone" width="150"></el-table-column>
				<el-table-column label="发送时间" prop="sendTime" width="150"></el-table-column>
				<el-table-column label="发送状态" prop="status" width="100">
					<template #default="scope">
						<el-button type="danger" size="small" v-if="scope.row.status==='0'">发送中</el-button>
						<el-button type="info" size="small" v-else>发送失败</el-button>
					</template>
				</el-table-column>
				<el-table-column label="消息内容" prop="content" width="400"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="120">
					<template #default="scope">
						<el-button-group>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
import scSelectFilter from '@/components/scSelectFilter'
import saveDialog from './save'
export default {
	name: 'Mobile Sms',
	components: {
		scSelectFilter,
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false
			},
			apiObj: this.$API.system.mobilesms.getMySendMobileSms,
			filterData: [
				{
					title: "日期范围",
					key: "date",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "今日",
							value: "1"
						},
						{
							label: "昨日",
							value: "2"
						},
						{
							label: "一周内",
							value: "3"
						},
						{
							label: "一月内",
							value: "4"
						},
						{
							label: "一月前",
							value: "5"
						}
					]
				},
				{
					title: "消息类型",
					key: "module",
					multiple: false,
					options: [{
						label: "全部",
						value: ""
					},
						{
							label: "我发送的",
							value: "1"
						},
						{
							label: "我接收的",
							value: "2"
						}
					]
				}
			],
			selection: []
		}
	},
	mounted() {

	},
	methods: {
		sendSms(){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		filterChange(data) {
			this.$refs.table.upData(data)
		},
		//删除
		async table_del(row, index) {
			var res = await this.$API.system.mobilesms.deleteMobileSms.post({smsId: row.smsId});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async () => {
				const loading = this.$loading();
				var idsArr = [];
				this.selection.forEach(item => {
					idsArr.push(item.smsId)
				})
				var res = await this.$API.system.mobilesms.deleteMobileSmsByIds.post({ids: idsArr});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//本地更新数据
		handleSuccess() {
			this.$refs.table.refresh()
		}
	}
}
</script>

<style>
</style>
